/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ui;

import ez430.Watch;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingWorker;

/**
 *
 * @author georg
 */
public class AccWorker extends SwingWorker{
    ez430ui instance;
    Watch watch;
    public boolean paused = true;
    private final int delayBetweenGestures = 500;

    public AccWorker(ez430ui instance, Watch watch){
        this.instance = instance;
        this.watch = watch;
    }

    @Override
    protected Object doInBackground() throws Exception {
            try {
                long gestureNextTime = 0, gesturePrevTime = 0;

                while (true) {
                    if(this.isCancelled())
                        break;
                    
                    synchronized(this){
                        if(paused)
                            wait();
                    }

                    byte[] valuesX = new byte[3];
                    byte[] valuesY = new byte[3];
                    byte[] valuesZ = new byte[3];

                    for(int i = 0; i < 3; i++){
                        byte[] data = null;
                        data = watch.sendGetAccData();
                        if(data != null){
                            valuesX[i] = data[0];
                            valuesY[i] = data[1];
                            valuesZ[i] = data[2];
                        }
                    }

                    int x = (valuesX[0] + valuesX[1] + valuesX[2]) / 3;
                    int y = (valuesY[0] + valuesY[1] + valuesY[2]) / 3;
                    int z = (valuesZ[0] + valuesZ[1] + valuesZ[2]) / 3;


                    instance.setTxtX(Integer.toString(x));
                    instance.setTxtY(Integer.toString(y));
                    instance.setTxtZ(Integer.toString(z));

                    int maxX = Integer.parseInt(instance.getTxtMaxX());
                    int maxY = Integer.parseInt(instance.getTxtMaxY());
                    int minX = Integer.parseInt(instance.getTxtMinX());
                    int minY = Integer.parseInt(instance.getTxtMinY());


                    /*
                     * Zurück
                     */
                    if(x > maxX){
                        long currentTime = System.currentTimeMillis();

                        if((currentTime - gesturePrevTime) > delayBetweenGestures){
                            instance.setChkMaxX(true);
                            System.out.println("Gesture Prev");
                            Process p = Runtime.getRuntime().exec("./xdo.sh Left");
                        }

                        gesturePrevTime = System.currentTimeMillis();

                    }
                    else
                        instance.setChkMaxX(false);

                    if(y > maxY)
                        instance.setChkMaxY(true);
                    else
                        instance.setChkMaxY(false);

                    /*
                     * Weiter
                     */
                    if(x < minX){
                        long currentTime = System.currentTimeMillis();
                        
                        if((currentTime - gestureNextTime) > delayBetweenGestures){
                            instance.setChkMinX(true);
                            System.out.println("Gesture Next");
                            Runtime.getRuntime().exec("./xdo.sh Right");
                        } 
                        
                        gestureNextTime = System.currentTimeMillis();
                    }
                    else
                        instance.setChkMinX(false);

                    if(y < minY)
                        instance.setChkMinY(true);
                    else
                        instance.setChkMinY(false);
                }
            } catch (Exception ex) {
                Logger.getLogger(ez430ui.class.getName()).log(Level.SEVERE, null, ex);
            }

            return null;
    }

}
